const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes, Op} = require("../../models/db");

router.post('/scan/getHistBackById', async ctx => {
    let billId = ctx.request.body.billId;
    let hist = await db.models['invoiceBack'].findAll({
        where: {
            oldIid: billId
        }
    })
    if (hist.length === 0) {
        return ctx.suc('', hist);
    }
    let iids = [];
    hist.map(it => {
        iids.push(it.iid)
    })
    let invoice = await db.models['invoice'].findAll({
        where: {
            id:{
                [Op.in]: iids
            }
        },
        attributes: ['id','billNo','arrears','transTypeName']
    })
    invoice = invoice.toJSON()
    let infos = await db.models['invoiceInfo'].findAll({
        where: {
            iid: {
                [Op.in]: iids
            }
        }
    })
    invoice.map(it => {
        let id = it.id
        it.infos = []
        infos.map(item => {
            let iid = item.iid
            if(id === iid) {
                it.infos.push(item)
            }
        })
    })

    return ctx.suc('', invoice);
})



module.exports = router;
